package com.ruoyi.project.cms.product.controller;

import com.ruoyi.common.constant.ProductConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.product.domain.Product;
import com.ruoyi.project.cms.product.domain.ProductPic;
import com.ruoyi.project.cms.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime 2020/5/18 22:01
 * 产品菜单管理
 */
@Controller
@RequestMapping("/product/menu")
public class ProductController extends BaseController {
    //指定一个全局地址前缀
    private String prefix = "/cms/product/menu";

    @Autowired
    private ProductService productService;

    //页面主体跳转
//    @RequiresPermissions("system:post:view") 权限过滤后期再处理
    @GetMapping("")
    public String menu()
    {
        return prefix + "/productMenu";
    }

    /**
     * 主页面菜单显示
     */
//    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.findAll(product);
        return getDataTable(list);
    }

    /**
     * 增加一个产品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增产品
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addProduct(@Validated Product product)
    {
        if (ProductConstants.PRODUCT_NAME_NOT_UNIQUE.equals(productService.checkPnameUnique(product)))
        {
            return error("新增产品'" + product.getPname() + "'失败，产品名称已存在");
        }else if (product.getProductMaterial().getMaterialId() == null ||
                product.getProductClass().getClassId() == null ||
                product.getProductSeries().getSeriesId() == null ||
                product.getProductSpec().getSpecId() == null ||
                product.getProductStyle().getStyleId() == null){
            return error("新增产品'" + product.getPname() + "'失败，必须选择产品的系列、风格、规格、类型、材质属性");
        }
        return toAjax(productService.addProduct(product));
    }
    /**
     * 校验产品名称checkProProp
     */
    @PostMapping("/checkPnameUnique")
    @ResponseBody
    public String checkPnameUnique(Product product)
    {
        return productService.checkPnameUnique(product);
    }

    /**
     * 查看产品的具体信息
     */
    @GetMapping("/detail/{pid}")
    public String detailProduct(@PathVariable("pid") Long pid, ModelMap mmap)
    {
        mmap.put("pro",productService.findById(pid));
        return prefix + "/detail";
    }

    /**
     * 根据id查询
     *    数据回显
     */
    @GetMapping("/edit/{pid}")
    public String editProduct(@PathVariable("pid") Long pid, ModelMap mmap)
    {
        mmap.put("pro",productService.findById(pid));
        return prefix + "/edit";
    }
    /**
     * 修改保存产品
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSaveProduct(@Validated Product product)
    {
        if (ProductConstants.PRODUCT_NAME_NOT_UNIQUE.equals(productService.checkPnameUnique(product)))
        {
            return error("修改产品'" + product.getPname() + "'失败，产品名称已存在");
        }else if (product.getProductMaterial().getMaterialId() == null ||
                product.getProductClass().getClassId() == null ||
                product.getProductSeries().getSeriesId() == null ||
                product.getProductSpec().getSpecId() == null ||
                product.getProductStyle().getStyleId() == null){
            return error("修改产品'" + product.getPname() + "'失败，必须选择产品的系列、风格、规格、类型、材质属性");
        }
        return toAjax(productService.changeProduct(product));
    }

    /**
     * 删除产品
     */
//    @RequiresPermissions("system:post:remove")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeProducts(String ids)
    {
        try
        {
            return toAjax(productService.removeProductByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
